package saint.manage;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;

public class Convert {
	// Unit Type
	public static final int MILES_PER_HOUR = 0;
	public static final int MILES_PER_SEC = 1;
	public static final int YARD_PER_SEC = 2;
	public static final int FEET_PER_SEC = 3;
	public static final int KM_PER_HOUR = 4;
	public static final int KM_PER_SEC = 5;
	public static final int M_PER_SEC = 6;
	public static final int KNOT = 7;

	public static final int METER = 0;
	public static final int KILOMETER = 1;
	public static final int MILE = 2;
	public static final int YARD = 3;
	public static final int FOOT = 4;
	// Unit factor
	public static final double SPEED_FACTOR[] = { 2.2369, 0.0006, 1.0936,
			3.2808, 3.6, 0.001, 1, 1.9438 };
	public static final double DISTANCE_FACTOR[] = { 1, 0.001, 0.0006, 1.094,
			3.281 };
	// Unit string
	public static final String SPEED_UNIT_STRING[] = { "mi/h", "mi/s", "yd/s",
			"ft/s", "km/h", "km/s", "m/s", "kn" };
	public static final String DISTANCE_UINT_STRING[] = { "m", "km", "mile",
			"yd", "ft" };

	// Unit convert method
	public static double ConvertSpeed(float value, int convert_type) {
		double speed;
		if (convert_type > 7) {
			return 0;
		}
		speed = value * SPEED_FACTOR[convert_type];
		return speed;
	}

	public static double ConvertDistance(double value, int convert_type) {
		double distance;
		if (convert_type > 4) {
			return 0;
		}
		distance = value * DISTANCE_FACTOR[convert_type];
		return distance;

	}
	
	/**
	 * This method converts dp unit to equivalent device specific value in pixels. 
	 * 
	 * @param dp A value in dp(Device independent pixels) unit. Which we need to convert into pixels
	 * @param context Context to get resources and device specific display metrics
	 * @return A float value to represent Pixels equivalent to dp according to device
	 */
	public static int dpToPx(int dp,Context context) {
		Resources resource = context.getResources();
		DisplayMetrics metrics = resource.getDisplayMetrics();
		int px = (int) (dp * metrics.density);
		return px;
	}
	
	/**
	 * This method converts device specific pixels to device independent pixels.
	 * 
	 * @param px A value in px (pixels) unit. Which we need to convert into db
	 * @param context Context to get resources and device specific display metrics
	 * @return A float value to represent db equivalent to px value
	 */
	public static int pxToDp(int px,Context context){
	    Resources resources = context.getResources();
	    DisplayMetrics metrics = resources.getDisplayMetrics();
	    int dp = (int) (px / (metrics.densityDpi / 160f));
	    return dp;
	}
}
